package com.qyl.honghengshop.controller.admin;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.Goods;
import com.qyl.honghengshop.service.GoodsService;
import com.qyl.honghengshop.service.impl.GoodsServiceImpl;
import com.qyl.honghengshop.utils.FileUpload;
import com.qyl.honghengshop.utils.PageBean;

@Controller
@RequestMapping("/admin/goods")
public class GoodsController {

	private GoodsService goodsService = new GoodsServiceImpl();
	
	@RequestMapping("/goodsList")
	public String shopList(Integer pageIndex,Integer pageSize,ModelMap map,Goods goods){
		
		//1.计算总页数
		int recordCount = goodsService.getRecordCount(goods);
		
		//2.创建pageBean
		PageBean pageBean = new PageBean(pageIndex, recordCount,pageSize);
		
		//获得有记录集合的pageBean
		goodsService.shopTypeList(pageBean,goods);
		
		map.put("pageBean", pageBean);
		
		//回显查询数据
		map.put("goods", goods);
		
		return "goods/goodsList";		
	}
	
	@RequestMapping("/toAdd")
	public String toAdd(){

		return "goods/goodsAdd";
	}
	
	@RequestMapping("/add")
	public String add(MultipartFile shopPic,Goods goods,
			HttpSession session,ModelMap map) throws Exception{
		
		String fileName = FileUpload.fileUpload(shopPic);
		
		//补全商品信息
		goods.setImage(fileName);
		goods.setCreate_date(new Date());
		goods.setGrounding_date(new Date());
		//调用service保存数据到数据库
		int count = goodsService.saveShop(goods);

		if(count>0){
			map.put(PageConstant.TIP, "保存成功");
		}else{
			map.put(PageConstant.TIP, "保存失败");			
		}
		
		return "goods/goodsAdd";
	}
	@RequestMapping("/delgoods")
	public String delgoods(Integer id,ModelMap map){
		
		int count = goodsService.delShop(id);
		
		if(count>0){
			map.put(PageConstant.TIP, "删除成功");
		}else{
			map.put(PageConstant.TIP, "删除失败");			
		}
		
		return "forward:goodsList";
	}
	
	@RequestMapping("/togoodsUpdate")
	public String toShopUpdate(Integer id,ModelMap map){
		
		Goods good = goodsService.getShopById(id);
		
		map.put("good", good);
		
		return "goods/goodsUpdate";
		
	}
	
	@RequestMapping("/update")
	public String update(MultipartFile shopPic,Goods goods,
			HttpSession session,ModelMap map) throws Exception{
				
		String fileName = FileUpload.fileUpload(shopPic);
		
		//补全商品信息
		goods.setImage(fileName);
		goods.setCreate_date(new Date());
		goods.setGrounding_date(new Date());
		//调用service保存数据到数据库
		int count = goodsService.updateShop(goods);

		if(count>0){
			map.put(PageConstant.TIP, "更新成功");
		}else{
			map.put(PageConstant.TIP, "更新失败");			
		}
		
		return "forward:togoodsUpdate";
	}
	
}
